<template>
  <div class="enterpriseBasic">
    <div class="search">
      <el-form model="form" inline>
        <el-form-item label="企业名称">
          <el-input placeholder="请输入" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="工商注册号">
          <el-input placeholder="请输入" v-model="form.registrationNo"></el-input>
        </el-form-item>
        <el-form-item label="法定代表人">
          <el-input placeholder="请输入" v-model="form.legalRepresentative"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="add">
      <el-button type="primary" @click="addEnterprise">添加企业</el-button>
    </div>
    <div class="table">
      <el-table :data="tableData" style="height: calc(100vh - 230px)">
        <el-table-column width="60" type="index" label="序号"></el-table-column>
        <template v-for="item in tableColumn" :key="item.prop">
          <template v-if="!item.custom">
            <el-table-column
              :prop="item.prop"
              :label="item.label"
              :align="item.align"
              :fixed="item.fixed"
              :show-overflow-tooltip="item.showOverflowTooltip"
            ></el-table-column>
          </template>
          <template v-else>
            <el-table-column
              v-if="item.prop === 'establishTime'"
              :label="item.label"
              :align="item.align"
              :fixed="item.fixed"
              :show-overflow-tooltip="item.showOverflowTooltip"
            >
              <template v-slot:default="scope">
                <span>{{ new Date(scope.row.establishTime).toLocaleString() }}</span>
              </template>
            </el-table-column>
          </template>
        </template>
        <el-table-column align="center" prop="operation" label="操作">
          <template #default="scope">
            <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div v-if="tableData.length" class="pagination-w">
        <el-pagination
          :total="searchParams.total"
          background
          :locale="zhCn"
          :current-page="searchParams.current"
          :page-size="searchParams.pageSize"
          :page-sizes="searchParams.pageSizes"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
    <AddEnterpriseDialog ref="addEnterpriseDialog" @submit="getEnterpriseData" />
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue'
import { getEnterpriseList, deleteEnterprise } from '@/api/enterprise'
import AddEnterpriseDialog from './addEnterpriseView.vue'
import { ElMessage } from 'element-plus'

const form = reactive({
  name: '',
  registrationNo: '',
  legalRepresentative: '',
})
const tableColumn = ref([
  {
    prop: 'name',
    label: '企业名称',
    align: 'center',
    showOverflowTooltip: true,
  },
  {
    prop: 'registrationNo',
    label: '工商注册号',
    align: 'center',
    showOverflowTooltip: true,
  },
  {
    prop: 'legalRepresentative',
    label: '法定代表人',
    align: 'center',
    showOverflowTooltip: true,
  },
  {
    prop: 'registeredCapital',
    label: '注册资本',
    align: 'center',
    showOverflowTooltip: true,
  },
  {
    prop: 'registeredAddress',
    label: '注册地址',
    align: 'center',
    showOverflowTooltip: true,
  },
  {
    prop: 'operationStatus',
    label: '经营状态',
    align: 'center',
    showOverflowTooltip: true,
  },
  {
    prop: 'establishTime',
    label: '注册时间',
    custom: true,
    align: 'center',
    showOverflowTooltip: true,
  },
])
const tableData = ref([])
const searchParams = ref({
  current: 1,
  pageSize: 10,
  pageSizes: [10, 20, 50, 100, 200],
  total: 0,
})
const addEnterpriseDialog = ref(null)
const handleSizeChange = (newSize) => {
  console.log('size change', newSize)
  searchParams.value.pageSize = newSize
  getEnterpriseData()
}

const handlePageChange = (newPage) => {
  console.log('page change', newPage)
  searchParams.value.current = newPage
  getEnterpriseData()
}

const search = async () => {
  const res = await getEnterpriseList(
    form.name,
    form.registrationNo,
    form.legalRepresentative,
    searchParams.value.current,
    searchParams.value.pageSize
  )
  if (res.status === 201 || res.status === 200) {
    tableData.value = res.data.data.enterprises
    searchParams.value.total = parseInt(res.data.data.totalCount) || 0
  }
}
const getEnterpriseData = async () => {
  const res = await getEnterpriseList(
    form.name,
    form.registrationNo,
    form.legalRepresentative,
    searchParams.value.current,
    searchParams.value.pageSize
  )
  if (res.status === 201 || res.status === 200) {
    tableData.value = res.data.data.enterprises
    searchParams.value.total = parseInt(res.data.data.totalCount) || 0
  }
}

const addEnterprise = () => {
  addEnterpriseDialog.value?.openDialog()
}
const handleEdit = (row) => {
  addEnterpriseDialog.value?.openDialog(row)
}

const handleDelete = async (id) => {
  const res = await deleteEnterprise(id)
  if (res.status === 201 || res.status === 200) {
    ElMessage.success('删除成功')
    getEnterpriseData()
  } else {
    ElMessage.error(res.data.data || '系统繁忙，请稍后再试')
  }
}
onMounted(() => {
  getEnterpriseData()
})
</script>

<style scoped lang="scss">
.enterpriseBasic {
  padding: 30px;
  position: relative;
  .add {
    position: absolute;
    top: 30px;
    right: 30px;
  }
  .table {
    .pagination-w {
      display: flex;
      justify-content: right;
      margin-top: 20px;
    }
  }
}
</style>

